// 校验角色的未分配/总属性点与当前等级是否匹配
// 规则：每升1级新增4点“可分配属性点”（availablePoints）+ 四维基础按业务已分配累计

const mysql = require('mysql2/promise');
const config = require('./database-config.js');

async function main() {
    const conn = await mysql.createConnection(config.source);
    try {
        console.log('🔍 开始校验角色属性点与等级是否匹配...');
        // 读取角色与属性点
        const [rows] = await conn.execute(
            `SELECT c.id, c.name, c.lvl, c.trans,
              ap.strength, ap.constitution, ap.intelligence, ap.agility, ap.availablePoints
         FROM characters c
    LEFT JOIN character_attribute_points ap ON ap.characterId = c.id`
        );

        if (!rows || rows.length === 0) {
            console.log('✅ 没有角色数据');
            return;
        }

        const mismatches = [];
        for (const r of rows) {
            const level = r.lvl || 0;
            const s = r.strength || 0;
            const con = r.constitution || 0;
            const intel = r.intelligence || 0;
            const agi = r.agility || 0;
            const avail = r.availablePoints || 0;

            // 按规则：总点数应为 level * 4
            const expectedTotal = level * 4;
            const actualTotal = s + con + intel + agi + avail;

            if (actualTotal !== expectedTotal) {
                mismatches.push({
                    id: r.id,
                    name: r.name,
                    lvl: level,
                    trans: r.trans || 0,
                    strength: s,
                    constitution: con,
                    intelligence: intel,
                    agility: agi,
                    availablePoints: avail,
                    expectedTotal,
                    actualTotal,
                    delta: expectedTotal - actualTotal,
                });
            }
        }

        if (mismatches.length === 0) {
            console.log('✅ 所有角色属性点与等级匹配');
        } else {
            console.log(`⚠️  共发现 ${mismatches.length} 个不匹配角色：`);
            for (const m of mismatches) {
                console.log(`- 角色ID=${m.id}, 名称=${m.name}, 等级=${m.lvl}, ` +
                    `total=${m.actualTotal}, expected=${m.expectedTotal}, 差额=${m.delta}, ` +
                    `四维(${m.strength},${m.constitution},${m.intelligence},${m.agility}), p=${m.availablePoints}`);
            }
        }
    } catch (e) {
        console.error('❌ 校验失败:', e.message || e);
        process.exitCode = 1;
    } finally {
        // @ts-ignore
        await conn.end();
    }
}

if (require.main === module) {
    main();
}


